﻿using BusinessIntelligence;
using Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sox;

namespace Service
{
    public  class Methods
    {
        public static Logging log = new Logging();
        public BusinessIntelligence.BusinessIntelligence bi = new BusinessIntelligence.BusinessIntelligence();

        #region Service Implementation
        public  LoginServiceResponse PerformLoginProcess(LoginRequest request)
        {
            log.EnterMethod(request.Log(),"Service");
            //Be van lépve?
            if (!IsOnline())
            {
                LoginServiceResponse result = new LoginServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            if(IsLogged(request.UserName))
            {
                LoginServiceResponse result = new LoginServiceResponse(203);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            LoginBIResponse response = bi.LoginProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }

        public  LogoutServiceResponse PerformLogoutProcess(LogoutRequest request)
        {
            log.EnterMethod(request.Log(), "Service");
            //Be van lépve?
            if (!IsOnline())
            {
                LogoutServiceResponse result = new LogoutServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            LogoutBIResponse response = bi.LogoutProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }

        public  GetMsgServiceResponse PerformGetMsgProcess(GetMsgRequest request)
        {
            log.EnterMethod(request.Log(), "Service");
            if (!IsOnline())
            {
                GetMsgServiceResponse result = new GetMsgServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            GetMsgBIResponse response = bi.GetMsgProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }

        public  CloseGameServiceResponse PerformCloseGameProcess(CloseGameRequest request)
        {
            log.EnterMethod(request.Log(), "Service");
            //Játék él még?
            if (!IsOnline())
            {
                CloseGameServiceResponse result = new CloseGameServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            if (!IsGameAlive(request.TableID))
            {
                CloseGameServiceResponse result = new CloseGameServiceResponse(207);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            CloseGameBIResponse response = bi.CloseGameProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }

        public  GiveUpServiceResponse PerformGiveUpProcess(GiveUpRequest request)
        {
            log.EnterMethod(request.Log(), "Service");
            //Játék él még?
            if (!IsOnline())
            {
                GiveUpServiceResponse result = new GiveUpServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            if (!IsGameAlive(request.TableID))
            {
                GiveUpServiceResponse result = new GiveUpServiceResponse(207);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            GiveUpBIResponse response = bi.GiveUpProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }

        public  JoinGameServiceResponse PerformJoinGameProcess(JoinGameRequest request)
        {
            log.EnterMethod(request.Log(), "Service");
            //Játék él még?
            if (!IsOnline())
            {
                JoinGameServiceResponse result = new JoinGameServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            if (!IsGameAlive(request.TableID))
            {
                JoinGameServiceResponse result = new JoinGameServiceResponse(204);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            JoinGameBIResponse response = bi.JoinGameProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }

        public  ListGameServiceResponse PerformListGameProcess(ListGameRequest request)
        {
            log.EnterMethod(request.Log(), "Service");
            if (!IsOnline())
            {
                ListGameServiceResponse result = new ListGameServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            ListGameBIResponse response = bi.ListGameProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }

        public  NewGameServiceResponse PerformNewGameProcess(NewGameRequest request)
        {
            log.EnterMethod(request.Log(), "Service");
            //Vendég nem indíthat játékot
            if (!IsOnline())
            {
                NewGameServiceResponse result = new NewGameServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            /*if(!IsGuest(request.Username))
            {
                NewGameServiceResponse result = new NewGameServiceResponse(211);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }*/
            NewGameBIResponse response = bi.NewGameProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }


        public  PlayerStatsServiceResponse PerformPlayerStatsProcess(PlayerStatsRequest request)
        {
            log.EnterMethod(request.Log(), "Service");
            if (!IsOnline())
            {
                PlayerStatsServiceResponse result = new PlayerStatsServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            PlayerStatsBIResponse response = bi.PlayerStatsProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }


        public  RegistrationServiceResponse PerformRegistrationProcess(RegistrationRequest request)
        {
            log.EnterMethod(request.Log(), "Service");
            if (!IsOnline())
            {
                RegistrationServiceResponse result = new RegistrationServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            RegistrationBIResponse response = bi.RegistrationProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }

        public  SendMsgServiceResponse PerformSendMsgProcess(SendMsgRequest request)
        {
            log.EnterMethod(request.Log(), "Service");
            //Vendég nem küldhet üzenetet
            if (!IsOnline())
            {
                SendMsgServiceResponse result = new SendMsgServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            /*if (!IsGuest(request.UserName))
            {
                SendMsgServiceResponse result = new SendMsgServiceResponse(211);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }*/
            SendMsgBIResponse response = bi.SendMsgProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }

        public  StepServiceResponse PerformStepProcess(StepRequest request)
        {
            log.EnterMethod(request.Log(), "Service");
            if (!IsOnline())
            {
                StepServiceResponse result = new StepServiceResponse(209);
                log.LeaveMethod(result.Log(), "Service");
                return result;
            }
            StepBIResponse response = bi.StepProcess(request.FromDto());
            log.LeaveMethod(request.Log(), "Service");
            return response.ToDto();
        }

        #endregion

        #region Checking Datas

        private  bool IsOnline()
        {
            ServerServices so = new ServerServices();
            return so.IsOnline();            
        }

        private  bool IsLogged(string Username)
        {
            return bi.IsLogged(Username);
        }

        /*private  bool IsGuest(string Username)
        {
            return Username.Contains("Guest");
        }*/

        private  bool IsGameAlive(string ID)
        {
            return bi.IsGameAlive(ID);
        }

        /*internal  GameResultServiceResponse PerformGameResultProcess(GameResultRequest request)
        {
            GameResultBIResponse response = new GameResultBIResponse();
            response = bi.GameResult(request.FromDto());
            return response.ToDto();
        }*/

        /*internal  StartGameServiceResponse PerformStartGameProcess(StartGameRequest request)
        {
            StartGameBIResponse response = new StartGameBIResponse();
            response = bi.StartGame(request.FromDto());
            return response.ToDto();
        }*/

        #endregion
    }
}
